library(lattice)
library(psych)
library(MASS)
library(foreign)
library(mokken)
library(gridExtra)
library(car)
library(effects)
library(haven)
# For 2016
#setwd() ## SET WORKING DIRECTORY
values.data  <- as.data.frame(read_dta("anes1620panel_coded.dta"))
valuequestions <- na.omit(values.data[c(3675,3677,3679,3681,3683,3685)])
head(valuequestions)
sumrate <- as.data.frame(scale(valuequestions))

scale <- apply(valuequestions, MARGIN = 1, FUN = mean)
summary(scale)
sd(scale)

histogram(~scale, 
          aspect = 1,
          main = "2016",
          ylim = c(0,29),
          xlab = "Six-Item Summated Rating Scale"
)

restscores <- matrix(nrow = nrow(valuequestions), ncol = ncol(valuequestions))
names <- rep("rest", times = ncol(valuequestions))
for (j in 1:ncol(valuequestions)) {
  restscores[, j] <- apply(valuequestions[, -j], MARGIN = 1, FUN = mean)
  names[j] <- paste(names[j], colnames(valuequestions)[j], sep = ".")
}
colnames(restscores) <- names

# Egalitarianism Item #1
set.seed(1234)
xyplot(valuequestions[,1] ~ restscores[,1],
       main = "2016",
       aspect = 1,
       ylab = "Equalopp",
       xlab = "Rest Scores",
       panel = function (x, y) {
         panel.xyplot(jitter(x), jitter(y), col = "gray")
         panel.loess(x, y, col = "black")
         panel.lmline(x, y, col = "red")
       }       
)

dev.off()


# Egalitarianism Item #2
set.seed(1234)
xyplot(valuequestions[,2] ~ restscores[,2],
       main = "2016",
       aspect = 1,
       ylab = "Lessequal",
       xlab = "Rest Scores",
       panel = function (x, y) {
         panel.xyplot(jitter(x), jitter(y), col = "gray")
         panel.loess(x, y, col = "black")
         panel.lmline(x, y, col = "red")
       }       
)

dev.off()


# Egalitarianism Item #3
set.seed(1234)
xyplot(valuequestions[,3] ~ restscores[,3],
       main = "2016",
       aspect = 1,
       ylab = "Unequal",
       xlab = "Rest Scores",
       panel = function (x, y) {
         panel.xyplot(jitter(x), jitter(y), col = "gray")
         panel.loess(x, y, col = "black")
         panel.lmline(x, y, col = "red")
       }       
)

dev.off()


# Egalitarianism Item #4
set.seed(1234)
xyplot(valuequestions[,4] ~ restscores[,4],
       main = "2016",
       aspect = 1,
       ylab = "Fewer",
       xlab = "Rest Scores",
       panel = function (x, y) {
         panel.xyplot(jitter(x), jitter(y), col = "gray")
         panel.loess(x, y, col = "black")
         panel.lmline(x, y, col = "red")
       }       
)

dev.off()


# Moral Trad Item #1
set.seed(1234)
xyplot(valuequestions[,5] ~ restscores[,5],
       main = "2016",
       aspect = 1,
       ylab = "Changing",
       xlab = "Rest Scores",
       panel = function (x, y) {
         panel.xyplot(jitter(x), jitter(y), col = "gray")
         panel.loess(x, y, col = "black")
         panel.lmline(x, y, col = "red")
       }       
)

dev.off()


# Moral Trad Item #2
set.seed(1234)
xyplot(valuequestions[,6] ~ restscores[,6],
       main = "2016",
       aspect = 1,
       ylab = "Family",
       xlab = "Rest Scores",
       panel = function (x, y) {
         panel.xyplot(jitter(x), jitter(y), col = "gray")
         panel.loess(x, y, col = "black")
         panel.lmline(x, y, col = "red")
       }       
)

dev.off()


# For 2020
values.data  <- as.data.frame(read_dta("anes1620panel_coded.dta"))
valuequestions <- na.omit(values.data[c(3676,3678,3680,3682,3684,3686)])
head(valuequestions)
sumrate <- as.data.frame(scale(valuequestions))

scale <- apply(valuequestions, MARGIN = 1, FUN = mean)
summary(scale)
sd(scale)

histogram(~scale, 
          aspect = 1,
          main = "2020",
          ylim = c(0,29),
          xlab = "Six-Item Summated Rating Scale"
)

restscores <- matrix(nrow = nrow(valuequestions), ncol = ncol(valuequestions))
names <- rep("rest", times = ncol(valuequestions))
for (j in 1:ncol(valuequestions)) {
  restscores[, j] <- apply(valuequestions[, -j], MARGIN = 1, FUN = mean)
  names[j] <- paste(names[j], colnames(valuequestions)[j], sep = ".")
}
colnames(restscores) <- names

# Egalitarianism Item #1
set.seed(1234)
xyplot(valuequestions[,1] ~ restscores[,1],
       main = "2020",
       aspect = 1,
       ylab = "Equalopp",
       xlab = "Rest Scores",
       panel = function (x, y) {
         panel.xyplot(jitter(x), jitter(y), col = "gray")
         panel.loess(x, y, col = "black")
         panel.lmline(x, y, col = "red")
       }       
)

dev.off()


# Egalitarianism Item #2
set.seed(1234)
xyplot(valuequestions[,2] ~ restscores[,2],
       main = "2020",
       aspect = 1,
       ylab = "Lessequal",
       xlab = "Rest Scores",
       panel = function (x, y) {
         panel.xyplot(jitter(x), jitter(y), col = "gray")
         panel.loess(x, y, col = "black")
         panel.lmline(x, y, col = "red")
       }       
)

dev.off()


# Egalitarianism Item #3
set.seed(1234)
xyplot(valuequestions[,3] ~ restscores[,3],
       main = "2020",
       aspect = 1,
       ylab = "Unequal",
       xlab = "Rest Scores",
       panel = function (x, y) {
         panel.xyplot(jitter(x), jitter(y), col = "gray")
         panel.loess(x, y, col = "black")
         panel.lmline(x, y, col = "red")
       }       
)

dev.off()


# Egalitarianism Item #4
set.seed(1234)
xyplot(valuequestions[,4] ~ restscores[,4],
       main = "2020",
       aspect = 1,
       ylab = "Fewer",
       xlab = "Rest Scores",
       panel = function (x, y) {
         panel.xyplot(jitter(x), jitter(y), col = "gray")
         panel.loess(x, y, col = "black")
         panel.lmline(x, y, col = "red")
       }       
)

dev.off()


# Moral Trad Item #1
set.seed(1234)
xyplot(valuequestions[,5] ~ restscores[,5],
       main = "2020",
       aspect = 1,
       ylab = "Changing",
       xlab = "Rest Scores",
       panel = function (x, y) {
         panel.xyplot(jitter(x), jitter(y), col = "gray")
         panel.loess(x, y, col = "black")
         panel.lmline(x, y, col = "red")
       }       
)

dev.off()


# Moral Trad Item #2
set.seed(1234)
xyplot(valuequestions[,6] ~ restscores[,6],
       main = "2020",
       aspect = 1,
       ylab = "Family",
       xlab = "Rest Scores",
       panel = function (x, y) {
         panel.xyplot(jitter(x), jitter(y), col = "gray")
         panel.loess(x, y, col = "black")
         panel.lmline(x, y, col = "red")
       }       
)

dev.off()
